import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:shenling_logistics/utils/Auth.dart';
import 'package:shenling_logistics/utils/Http.dart';
import 'package:shenling_logistics/utils/Toast.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  // 账号用户名提示框
  final TextEditingController _accountController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  // 登录按钮是否高亮
  bool isActive = false;
  // 登录
  Future<void> login() async {
    try {
      // 非空校验
      String account = _accountController.text;
      if (account.isEmpty) {
        return CustomToast.error('请输入账号');
      }

      String password = _passwordController.text;
      if (password.isEmpty) {
        return CustomToast.error('请输入密码');
      }

      var res = await http.post('/driver/login/account', data: {
        'account': account,
        'password': password,
      });

      // 存储Token
      Auth().setToken(res['data']);

      // 提示用户
      CustomToast.success('登录成功');
      Navigator.pushNamed(context, '/');
    } catch (e) {
      CustomToast.error('登陆失败');
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text(
          '神领快递',
          style: TextStyle(
            fontWeight: FontWeight.w600,
            fontSize: 18,
            color: Colors.black87,
          ),
        ),
        centerTitle: true,
      ),
      body: Container(
        margin: EdgeInsets.only(top: 50, left: 33, right: 33),
        width: MediaQuery.of(context).size.width,
        // color: Colors.pink,
        child: Column(
          children: [
            Row(
              children: [
                Text(
                  '账号登录',
                  style: TextStyle(fontSize: 24, fontWeight: FontWeight.w600),
                ),
                Spacer(),
                Text(
                  '手机号登录',
                  style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      color: Colors.orange[400]),
                ),
                SvgPicture.asset(
                  'assets/ic_btn_more.svg',
                  colorFilter: ColorFilter.mode(Colors.orange, BlendMode.srcIn),
                )
              ],
            ),
            Container(
              margin: EdgeInsets.only(top: 45.5),
              child: Column(
                children: [
                  Container(
                    margin: EdgeInsets.only(bottom: 19),
                    child: TextField(
                      controller: _accountController,
                      decoration:
                          InputDecoration(labelText: '账号', hintText: '请输入账号'),
                    ),
                  ),
                  TextField(
                    obscureText: true,
                    controller: _passwordController,
                    onChanged: (value) {
                      // 判断是否高亮
                      setState(() {
                        isActive = value.length >= 6;
                      });
                    },
                    decoration:
                        InputDecoration(labelText: '密码', hintText: '请输入密码'),
                  ),
                ],
              ),
            ),
            Container(
                margin: EdgeInsets.only(top: 50),
                width: MediaQuery.of(context).size.width,
                height: 50,
                child: ElevatedButton(
                    style: ButtonStyle(
                        backgroundColor: WidgetStateProperty.all(
                            isActive ? Colors.red : Colors.red[100])),
                    onPressed: () {
                      login();
                    },
                    child: Text(
                      '登录',
                      style: TextStyle(fontSize: 18, color: Colors.white),
                    )))
          ],
        ),
      ),
    );
  }
}
